﻿namespace Oddo.Concept.Crm.Host.Console
{
    using System;
    using System.Web.Http.Controllers;
    using System.Web.Http.Dispatcher;
    using Microsoft.Practices.Unity;

    public class UnityControllerActivator : IHttpControllerActivator
    {
        private readonly IUnityContainer _container;
        private readonly DefaultHttpControllerActivator _defaultActivator;

        public UnityControllerActivator(IUnityContainer container)
        {
            this._container = container;
            _defaultActivator = new DefaultHttpControllerActivator();
        }

        public IHttpController Create(System.Net.Http.HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
        {
            try
            {
                return _container.Resolve(controllerType) as IHttpController;
            }
            catch (Exception)
            {
                return _defaultActivator.Create(request,controllerDescriptor, controllerType);
            }

        }
    }
}